import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import sessionsMock from './sessions'

// 创建模拟适配器实例
const mock = new MockAdapter(axios, { delayResponse: 1000 }) // 添加1秒延迟模拟网络请求

// 拦截会话管理相关API请求
// 获取会话列表
mock.onGet('/api/records/records').reply((config) => {
  const params = config.params || {}
  const response = sessionsMock.getRecords(params)
  return [200, response]
})

// 获取会话详情
mock.onGet(/\/api\/records\/records\/\d+/).reply((config) => {
  const id = parseInt(config.url.split('/').pop())
  const record = sessionsMock.getRecordById(id)
  if (record) {
    return [200, record]
  } else {
    return [404, { message: '会话不存在' }]
  }
})

// 删除会话
mock.onDelete(/\/api\/records\/records\/\d+/).reply((config) => {
  const id = parseInt(config.url.split('/').pop())
  return [200, { success: true, id }]
})

// 批量删除会话
mock.onPost('/api/records/records/batch').reply((config) => {
  const { ids, action } = JSON.parse(config.data)
  if (action === 'delete') {
    return [200, { success: true, deletedCount: ids.length }]
  }
  return [400, { message: '不支持的操作' }]
})

// 更新会话标签
mock.onPut(/\/api\/records\/records\/\d+\/tags/).reply((config) => {
  const id = parseInt(config.url.split('/').pop().replace('/tags', ''))
  const { tags } = JSON.parse(config.data)
  return [200, { id, tags }]
})

// 更新会话备注
mock.onPut(/\/api\/records\/records\/\d+\/notes/).reply((config) => {
  const id = parseInt(config.url.split('/').pop().replace('/notes', ''))
  const { notes } = JSON.parse(config.data)
  return [200, { id, admin_notes: notes }]
})

// 获取用户统计信息
mock.onGet(/\/api\/records\/users\/\d+\/stats/).reply(() => {
  return [200, sessionsMock.getUserStats()]
})

// 获取用户历史信息
mock.onGet(/\/api\/records\/users\/\d+\/history/).reply(() => {
  return [200, sessionsMock.getUserHistory()]
})

// 获取上一条记录
mock.onGet(/\/api\/records\/users\/\d+\/previous\/\d+/).reply((config) => {
  const currentId = parseInt(config.url.split('/').pop())
  return [200, sessionsMock.getPreviousRecord(null, currentId)]
})

// 获取下一条记录
mock.onGet(/\/api\/records\/users\/\d+\/next\/\d+/).reply((config) => {
  const currentId = parseInt(config.url.split('/').pop())
  return [200, sessionsMock.getNextRecord(null, currentId)]
})

// 分析相关接口
// 获取总览数据
mock.onGet('/api/records/analytics/overview').reply(() => {
  return [200, sessionsMock.getAnalyticsOverview()]
})

// 获取趋势数据
mock.onGet('/api/records/analytics/trends').reply((config) => {
  const { timeDimension } = config.params || {}
  return [200, sessionsMock.getAnalyticsTrends(timeDimension)]
})

// 获取用户分析数据
mock.onGet('/api/records/analytics/users').reply(() => {
  return [200, sessionsMock.getUserAnalytics()]
})

// 获取数据集分析数据
mock.onGet('/api/records/analytics/datasets').reply(() => {
  return [200, sessionsMock.getDatasetAnalytics()]
})

// 获取内容分析数据
mock.onGet('/api/records/analytics/content').reply(() => {
  return [200, sessionsMock.getContentAnalytics()]
})

// 获取模型性能数据
mock.onGet('/api/records/analytics/models').reply(() => {
  return [200, sessionsMock.getModelPerformance()]
})

export default mock
